//
//  YTArticleWebCell.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/8/15.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTArticleWebCell.h"
#import <WebKit/WebKit.h>
@interface YTArticleWebCell()<WKNavigationDelegate>
@property(nonatomic ,strong)WKWebView *webView; // 网页HTMl

@end
@implementation YTArticleWebCell
// lazy webView
- (WKWebView *)webView
{
    if (!_webView) {
        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
        
        // 自适应屏幕宽度js
        
        
//                WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
//                WKUserContentController *wkUController = [[WKUserContentController alloc] init];
//                [wkUController addUserScript:wkUScript];
        
        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 100) configuration:config];
      
        //config.userContentController = wkUController;
        _webView.scrollView.bounces = false;
        _webView.navigationDelegate = self;
        _webView.scrollView.scrollEnabled = false;
        [_webView setContentScaleFactor:1.0f];
        [_webView sizeThatFits:kScreenBounds.size];
        _webView.userInteractionEnabled = false;
 
    }
    return _webView;
}

- (void)dealloc
{
    [NSNotificationCenter.defaultCenter removeObserver:self];
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.webView];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setTextSize:(int)textSize
{
    _textSize = textSize;

    [self dealNewsDetail:self.post_content];
}


-(void)setPost_content:(NSString *)post_content
{
    if (!_post_content) {
        _post_content = post_content;
        
        [self dealNewsDetail:post_content];
    }
  
}

#pragma mark: - WKNavigationDelegate
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation
{
   
    NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta); var imgs = document.getElementsByTagName('img');for (var i in imgs){imgs[i].style.maxWidth='100%';imgs[i].style.height='auto';}";
    
    [webView evaluateJavaScript:jScript completionHandler:nil];

    //修改字体大小 300%
    NSString *changeTxt = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'",YTNetworkManager.shareManager.textSize];
    [webView evaluateJavaScript:changeTxt completionHandler:nil];
    
    __weak typeof(self) WeakSelf = self;

    
    [webView evaluateJavaScript:@"document.getElementById(\"iostest-div\").offsetTop" completionHandler:^(NSString *height, NSError * _Nullable error) {
        NSLog(@"%-----d",YTNetworkManager.shareManager.textSize);
        NSLog(@"%f",height.floatValue);

        WeakSelf.webView.frame = CGRectMake(self.webView.frame.origin.x,self.webView.frame.origin.y, kScreenWidth, height.floatValue);
           
        
        WeakSelf.reloadWebHeightBlock(height.floatValue);

    }];
  
 
    
}
- (void)dealNewsDetail:(NSString *)html
{
    html = [NSString stringWithFormat:@"<div style=\"text-align:justify; text-justify:inter-ideograph;\">%@",html];
    NSString *htmls = [NSString stringWithFormat:@"<html><head><meta http-equiv=\'Content-Type\' content=\'text/html; charset=utf-8\'/><meta content=\'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;\' name=\'viewport\' /><meta name=\'apple-mobile-web-app-capable\' content=\'yes\'><meta name=\'apple-mobile-web-app-status-bar-style\' content=\'black\'><link rel=\'stylesheet\' type=\'text/css\' /><style type=\'text/css\'> .color{color:#576b95;}</style></head><body><div id=\'content\'>%@</div>", html];
    
    [self.webView loadHTMLString:htmls baseURL:[NSURL URLWithString:@"http://cs.etaicj.com"]];
}

@end
